Drawing Text on the Screen
By: Rohan Varma
Introduction
This is a really short tutorial will walk you through how to draw text on the screen.
Drawing Text on the Screen
· In order to draw text on the screen, you need to have two global variables: A paint object and a string that will hold your text.
·
Somewhere in your program you will have to set consoleText to
something, otherwise your program will crash and burn. Other than that, in
onDraw add the following lines of code. These are settings that you can mess
with to alter how your text is drawn
·
Another useful method you can use is the Paint.getBounds() method,
which will tell you how big a Rect needs to be to show a string of text on the
screen.
·
The last thing to do is to draw the text using
canvas.drawText(String text, float x, float y, Paint paint). Float x is where
the left of the text will be and float y is where the top of the text will be.
You can change the x and y to do things like center the text (like in the code
below)